package com.sohoappspot.sohosched.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.sohoappspot.sohosched.client.model.Customer;
import com.sohoappspot.sohosched.client.rpc.CustomerService;
import com.sohoappspot.sohosched.client.rpc.CustomerServiceAsync;

public class CustomersGrid extends Composite {

	private static CustomersGridUiBinder uiBinder = GWT
			.create(CustomersGridUiBinder.class);

	interface CustomersGridUiBinder extends UiBinder<Widget, CustomersGrid> {
	}

	@UiField
	TextBox tbTest;
	@UiField
	Button btSubmit;
	@UiField
	Grid gridCustomers;
	@UiField
	Button btRefresh;

	private static int currentRow = 1;

	private CustomerServiceAsync customerSvc = GWT
			.create(CustomerService.class);

	public CustomersGrid() {
		initWidget(uiBinder.createAndBindUi(this));
		initGrid();
	}

	public void initGrid() {
		String[] columns = new String[] { "Name", "Options" };
		String[] styles = new String[] { "name", "options" };
		initTable(columns, styles, 1);
		refreshCustomerGrid();
	}

	@UiHandler("btSubmit")
	public void btSubmit_onSubmitClick(ClickEvent event) {
		addCustomerToDB(tbTest.getText());
	}

	@UiHandler("btRefresh")
	public void btRefresh_onRefreshClick(ClickEvent event) {
		initGrid();
	}

	private void addCustomerToDB(String customerName) {
		// Initialize the service proxy.
		if (customerSvc == null) {
			customerSvc = GWT.create(CustomerService.class);
		}
		
		final String thisCustomerName = customerName; 

		// Set up the callback object.
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
			public void onFailure(Throwable caught) {
				// TODO: Do something with errors.
			}
	
			public void onSuccess(Void result) {
				addCustomerToRow(thisCustomerName);				
			}
		};

		// Make the call to the stock price service.
		customerSvc.addCustomer(customerName, callback);

	}

	private void addCustomerToRow(String customerName) {
		// Insert new row
		final int thisRow = currentRow;
		gridCustomers.resize(currentRow + 1, 2);
		gridCustomers.setText(currentRow, 0, customerName);

		// Add a button to remove this stock from the table.
		Anchor removeStockButton = new Anchor("del");
		removeStockButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				gridCustomers.removeRow(thisRow);
				currentRow--;
			}
		});
		gridCustomers.setWidget(currentRow, 1, removeStockButton);

		currentRow++;
	}

	private void initTable(String[] columns, String[] columnStyles, int rowCount) {
		// Set up the header row. It's one greater than the number of visible
		// rows.

		removeRows();

		gridCustomers.resize(rowCount + 1, columns.length);
		for (int i = 0, n = columns.length; i < n; i++) {
			gridCustomers.setText(0, i, columns[i]);
			if (columnStyles != null) {
				gridCustomers.getCellFormatter().setStyleName(0, i,
						columnStyles[i] + " header");
			}
		}
	}

	private void removeRows() {
		// remove all rows
		// int rowCount = gridCustomers.getRowCount();
		int totalRows = gridCustomers.getRowCount();
		for (int i = 0; i < totalRows; i++) {
			gridCustomers.removeRow(0);
		}

		currentRow = 1;
	}

	private void refreshCustomerGrid() {
		// Initialize the service proxy.
		if (customerSvc == null) {
			customerSvc = GWT.create(CustomerService.class);
		}

		// Set up the callback object.
		AsyncCallback<Customer[]> callback = new AsyncCallback<Customer[]>() {
			public void onFailure(Throwable caught) {
				// TODO: Do something with errors.

			}

			public void onSuccess(Customer[] result) {
				// TODO Auto-generated method stub
				for (Customer eachCustomer : result) {
					addCustomerToRow(eachCustomer.getName());
				}
			}
		};

		// Make the call to the stock price service.
		customerSvc.getCustomers(callback);
	}

}
